/**
 *
 */
function TestSet(testFire)
{
    this.testFire = testFire;
    this.doInit();
    this.testAry = new Array();
    this.displayValue = "Test Set";
    this.description = "description";
    this.name = "test_set_name";
    this.package = "org.gettestfire.test";
    this.minVersion = {major: 0, minor: 0};
    this.maxVersion = {major: 0, minor: 0};
    this.setUp = new Test("setUp", "Setup procedures", this, true);
    this.tearDown = new Test("tearDown", "Tear Down procedures", this,  true);
    this.tests = new Array();
    this.engine = "IE";

}

/**
 *
 *
 */
TestSet.prototype = domplate(TestFireObject,
{
    displayValue: "Test Set",

    init: function()
    {
        
    },

    getTests: function()
    {
        var ary = new Array();
        ary.push(this.setUp);
        ary = ary.concat(this.tests);
        ary.push(this.tearDown);
        return ary;
    },

    getChildrenToDisplay: function()
    {
        var ary = new Array();
        ary.push({name: "name", object: this.name});
        ary.push({name: "package", object: this.package});
        ary.push({name: "userName", object: Firebug.TestFire.settings.username});
        ary.push({name: "description", object: this.description});
        ary.push({name: "engine", object: this.engine});
        ary.push({name: "maxVersion", object: this.maxVersion});
        ary.push({name: "minVersion", object: this.minVersion});
        ary.push({name: "setup", object: this.setUp});
        for(var i = 0, len = this.tests.length; i < len; ++i)
        {
            ary.push({name: this.tests[i].name, object: this.tests[i]});
        }
        ary.push({name: "tearDown", object: this.tearDown});
        return ary;
    },

    getContextMenuItems: function( object, target )
    {
        try
        {
            var items = [];
            items.push(
                "-",
            { nol10n: 1, label: "Add new Test", command: FBL.bindFixed(this.createTest, this) });
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

        return items;
    },

    open : function(element)
    {
        var attrs = element.attributes;
        this.copyAttributesToObject(this, attrs);
        /* MinVersion*/
        var min = element.getElementsByTagName("minVersion").item(0);
        this.copyAttributesToObject(this.minVersion, min.attributes);
        /* MaxVersion*/
        var max = element.getElementsByTagName("maxVersion").item(0);
        this.copyAttributesToObject(this.maxVersion, max.attributes);

        /* setUp */
        var setUpEl = element.getElementsByTagName("setUp").item(0);
        this.setUp = new Test("setUp", "Setup procedures", this, true);
        this.setUp.open(setUpEl, this);

        /* tearDown */
        var tearDownEl = element.getElementsByTagName("tearDown").item(0);
        this.tearDown = new Test("tearDown", "Tear down procedures", this, true);
        this.tearDown.open(tearDownEl, this);
        
//        /* Tests*/
        var elements = element.getElementsByTagName("test");

        for(var i = 0, L = elements.length; i < L; i++){
            var testEl = elements.item(i);
            var test = new Test('', '', this, false);
            test.open(testEl);
            this.tests.push(test);
        }


    },


    

    createTest: function()
    {
        try
        {
            var passThrough = this;
            var dlgFunc = {
                name: "test_" + passThrough.tests.length,
                description: 'Created by ' + Firebug.TestFire.settings.username,
                title: 'Create New Test',
                validate: function(field, value)
                {
                    if(field == 'name')
                    {
                        if(!value.match('^\\D\\w+'))
                        {
                            alert('Name must be a valid method name.'
                                + '\n\nASCII letters and numbers, no spaces, and starting'
                                + '\nwith a letter character.');
                            return false;
                        }
                        else{
                            return true;
                        }
                    }
                },
                accept: function( )
                {
                    try
                    {
                        passThrough.doCreate(this.name, this.description);
                    }
                    catch( ex )
                    {
                        Firebug.TestFire.util.tf_debug(ex);
                    }
                },
                cancel: function(){},
                set: function(field, value){
                    this[field] = value;
                }

            };
            window.openDialog("chrome://TestFireExtension/content/dialogs/setNameDialog.xul",
                "SetNameDialog", "chrome, dialog, modal, resizable=yes,centerscreen", dlgFunc);
//            FBL.
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }
    },

    setName : function(name)
    {
        this.name = name;
    },

    doCreate: function( name, desc )
    {
        if ( name && name.length > 0 )
        {
            var test = new Test(name, desc, this, false);
            this.tests.push(test);
            Firebug.TestFire.setSelectedTest(test);
            Firebug.TestFire.refreshAll();
        }
        else
        {
            alert("Name is required.");
            return false;
        }
        return true;
    },

    deleteTest : function(test)
    {
        var copy = new Array();
        for(var i = 0, len = this.tests.length; i< len; ++i)
        {
            if(this.tests[i] !== test)
            {
                copy.push(this.tests[i]);
            }
        }
        this.tests = copy;
        if(test == Firebug.TestFire.getSelectedTest())
        {
            Firebug.TestFire.setSelectedTest(this.setUp);
        }
        Firebug.TestFire.refreshAll();
    },

    toXMLStringAry: function()
    {

        try
        {
            var ret = new Array();
            var now = toYMDString(new Date());
            ret.push('<?xml version="1.0" encoding="UTF-8"?>');

            var serverUrl = '"TestFire http://www.gettestfire.org/schema/base.xsd"'
            var str = '<tf:TestSet '
                + 'created="' + now + '" '
                + 'lastVerified="' + now + '" '
                + 'description="' + this.description + '" '
                + 'name="' + this.name + '" '
                + 'package="' + this.package + '" '      
                + 'userName="' + Firebug.TestFire.settings.username + '" '
                + 'xsi:schemaLocation= ' + serverUrl + ' '
                + 'xmlns:tf="TestFire" '
                + 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
                + 'xmlns:wtj="watij" xmlns:wtjf="watij_finders" xmlns:wtjb="wtjb">';
            ret.push(str);
            ret.push('\t<tf:engine value="' + this.engine + '"/>');
            /* Versions*/
            ret.push('<tf:minVersion major="' + this.minVersion.major + '" minor="' + this.minVersion.minor
                + '"/>');
            ret.push('<tf:maxVersion major="' + this.maxVersion.major + '" minor="' + this.maxVersion.minor
                + '"/>');

            ret = ret.concat(this.setUp.toXMLStringAry('setUp'));
            for(var i = 0, len = this.tests.length; i < len; ++i)
            {
                ret = ret.concat(this.tests[i].toXMLStringAry());
            }
            /* Close the test set. */
            ret = ret.concat(this.tearDown.toXMLStringAry('tearDown'));

            
            ret.push('</tf:TestSet>');

        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }
        return  ret;
    },

    /**
     * Overrides toString in Object.
     */
        toString : function()
        {
            return this.name;
        }

});
